;BYSTRETKA ROSLINOZERNA
;NAJWAZNIEJSZE JEST NISZCZENIE DRZEW
;I JEDZENIE, NA KONCU UCIECZKA
;W RAZIE ZAGROŻENIA SZUKA MIESOZERNEJ I PROBUJE SIE DO NIEJ DOSTAC
;Argumenty
(deftemplate visible (slot Object) (slot PosX) (slot PosY))
(deftemplate field (slot Type) (slot Resource) (slot PosX) (slot PosY)) 
(deftemplate stat (slot Name) (slot Value))
;Wynik
(deftemplate move (slot Move) (slot MoveX) (slot MoveY))
;lokalne
(deftemplate musiszZjesc (slot Kiedy))
(deftemplate musiszUciec (slot PosX) (slot PosY))
(deftemplate schronienie (slot PosX) (slot PosY))
(deftemplate naCzymStoisz (slot nazwaPola) (slot wartoscPola))

(defrule zglodnialem
    (stat (Name food) (Value ?v) )
    (test (< ?v 15))
    =>
    (bind ?kiedy (- ?v 1))
    (assert (musiszZjesc (Kiedy ?kiedy)))
    
    )

(defrule sprawdzNaCzymStoisz
    (field(Type ?typPola)(Resource ?ileJednostek)(PosX 0)(PosY 0))
    (test (> ?ileJednostek 0))
    =>
    (assert (naCzymStoisz(nazwaPola ?typPola)(wartoscPola ?ileJednostek)))
    
    )

(defrule szukajZagrozenia
    (visible (Object DRAPIEZNIK) (PosX ?x)(PosY ?y))
    =>
    (assert (musiszUciec(PosX ?x)(PosY ?y)))
    )
(defrule szukajSchronienia
    (visible (Object BYSTRETKA_MIESOZERNA)(PosX ?x)(PosY ?y))
    =>
    (assert (schronienie(PosX ?x)(PosY ?y)))
    )
(defrule jedz
    (declare (salience 35))
    (musiszZjesc(Kiedy ?kiedy))
    (naCzymStoisz(nazwaPola FOOD)(wartoscPola ?r))
    (test (< ?kiedy 10))
    =>
    (printout t jem crlf) 
    (assert (move(Move collectFood)(MoveX 0)(MoveY 0)))
    )
(defrule idzPoZarcie
    (declare (salience 30))
    (field (Type food) (Resource ?r)(PosX ?x)(PosY ?y))
    (musiszZjesc(Kiedy ?kiedy))
    (test (< ?kiedy 10))
    =>
        (if (< ?x 0) then
        (assert  (move (Move move) (MoveX -1) (MoveY 0)))
        else (if (> ?x 0) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
            
        else (if (> ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
            
        else (if (< ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY -1)))))))
    )
(defrule zetnijDrzewo
    (declare (salience 26))
    (naCzymStoisz(nazwaPola TREE))
    =>
    (assert (move(Move cutTree)(MoveX 0)(MoveY 0)))
    (printout t scina_drzewo crlf)
    )
(defrule uciekaj
    (declare(salience 15))
    (musiszUciec(PosX ?x)(PosY ?y))
    =>
    (if (and (= ?x 0) (= ?y 1)) then
        (assert  (move (Move move) (MoveX 0) (MoveY -1)))
        else (if (and (= ?x 0) (= ?y -1)) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
            
        else (if (and (= ?x 1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX -1) (MoveY 0)))
            
        else (if (and (= ?x -1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
            ))))
    
    )
(defrule uciekajDoSchronienia
    (declare(salience 20))
    (musiszUciec(PosX ?x)(PosY ?y))
    (schronienie(PosX ?x2)(PosY ?y2))
    =>
      (if (and (= ?x 0) (= ?y 1)) then
        (assert  (move (Move move) (MoveX 0) (MoveY 1)))
        else (if (and (= ?x 0) (= ?y -1)) then
            (assert  (move (Move move) (MoveX 0) (MoveY -1)))
            
        else (if (and (= ?x 1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
            
        else (if (and (= ?x -1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX -1) (MoveY 0)))
            ))))
    )
(defrule idzDoDrzewa
    (declare (salience 25))
    (field (Type Tree)(Resource ?r)(PosX ?x)(PosY ?y))
    (test(> ?r 0))
    =>
    (if (< ?x 0) then
        	(assert  (move (Move move) (MoveX -1) (MoveY 0)))
        else (if (> ?x 0) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
        else (if (> ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
        else (if (< ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY -1)))))))
    )

